package com.hrt.sequence.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.Sequence;
import com.hrt.bean.UsersInfor;
import com.hrt.sequence.service.SequenceService;
import com.hrt.util.MD5Tool;
import com.hrt.util.PinYinUtil;

@Controller
@RequestMapping("/manage/sequence")
public class SequenceController extends BaseController<Sequence> {

	@Autowired
	private SequenceService service;

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		List<Sequence> resultList = new ArrayList<Sequence>();
		resultList = service.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(service.totalCount());

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		setList(resultList);
		page = this.getPage();

		view.addObject("pageResult", page);
		view.setViewName("manage/sequence/list");

		return view;
	}

	/**
	 * 
	 * @param pageIndex
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();
		List<Sequence> resultList = new ArrayList<Sequence>();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(service.totalCount());

		resultList = service.queryByPage(getPageIndex(), getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}

	
	@RequestMapping(value = "/queryPerfixBySeqName")
	@ResponseBody
	public String queryPerfixBySeqName(@RequestParam(value = "content", required = true) String content,
			HttpServletRequest request, HttpServletResponse response){
		
		String prefix=new String();
		if(content!=null && !"".equals(content)){
			try {
				
				content = new String(content.getBytes("iso-8859-1"),"utf-8");
				if(content!=null && !"".equals(content)){
					prefix=PinYinUtil.cn2FirstSpell(content)+"_";
				}
				return prefix;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
			
		}
		return null;
		
	}
	
	/**
	 * 
	 * @param seqId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) Long seqId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (seqId != null && !"".equals(seqId)) {
			// Map resultMap=new HashMap<K, V>();
			System.out.println("showshowshowshowshow " + seqId);
			Sequence sequence = this.service.getSequenceByid(seqId);
			if (sequence != null) {
				map = transBean2Map(sequence);
			}

		}
		return map;

	}

	/**
	 * 
	 * @param ids
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		ModelAndView view = new ModelAndView();

		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}

		}
		try {
			this.service.deleteById(idArr);

			return idArr;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("sequence") Sequence sequence,
			HttpServletRequest request, HttpServletResponse response) {
		
		System.out.println("save");

		try {
			if ((sequence.getPrefix() == null || "".equals(sequence
					.getPrefix()))
					|| (sequence.getSeqName() == null || "".equals(sequence
							.getSeqName()))||(sequence.getTableName() == null || "".equals(sequence
									.getTableName()))) {
				throw new Exception("信息不完整");
			}
			this.service.saveSequence(sequence);

			response.sendRedirect("list.do");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("sequence") Sequence sequence,
			HttpServletRequest request, HttpServletResponse response) {
		
		System.out.println("update");

		try {
			if(sequence!=null){
				
			if ((sequence.getPrefix() == null || "".equals(sequence
					.getPrefix()))
					|| (sequence.getSeqName() == null || "".equals(sequence
							.getSeqName()))||(sequence.getTableName() == null || "".equals(sequence
									.getTableName()))) {
				throw new Exception("信息不完整");
			}
			this.service.updateSequence(sequence);
			}
			

			response.sendRedirect("list.do");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}
